package com.smarthospital.common.util;

import com.alibaba.fastjson.JSON;
import com.squareup.okhttp.HttpUrl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Arrays;

/**
 * create by sym  2021/9/12 20:12.
 * 携带平台证书的客户端
 */
@Slf4j
public class SHWXHttpClient {
    private static RestTemplate restTemplate;
    static {
        restTemplate =  new RestTemplate();
    }
    public static  <T>T post(String url,Object param,Class<T> clazz) throws Exception{
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpUrl httpUrl= HttpUrl.get(new URI(url));
        httpHeaders.add("Authorization",WXSignUtil.getToken("POST",httpUrl,JSON.toJSONString(param)));
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON) );
        HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(param),httpHeaders);
        T data = restTemplate.postForObject(url, httpEntity, clazz);
        log.info("【微信支付】 out {}",JSON.toJSONString(data));
        return data;
    }

    public static  <T>T get(String url,Class<T> clazz) throws Exception{
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpUrl httpUrl= HttpUrl.get(new URI(url));
        httpHeaders.add("Authorization",WXSignUtil.getToken("GET",httpUrl,"{}"));
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON) );
        HttpEntity<String> httpEntity = new HttpEntity<>("parameters",httpHeaders);
        ResponseEntity<T> exchange = restTemplate.exchange(url, HttpMethod.GET, httpEntity, clazz);
        T data = exchange.getBody();
        log.info("【微信支付】 out {}",JSON.toJSONString(data));
        return data;
    }



}
